package com.qianyu.versioncontrolcore.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.versioncontrolcore.entity.bean.Area;
import com.qianyu.versioncontrolcore.entity.vo.AreaVo;
import com.qianyu.versioncontrolcore.mapper.AreaMapper;
import com.qianyu.versioncontrolcore.service.AreaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {
    @Override
    public List<AreaVo.AreaListVo> listArea() {
        LambdaQueryWrapper<Area> wrapper = Wrappers.lambdaQuery();
        wrapper.select(Area::getAdcode,Area::getName).eq(Area::getLevel,2)
                .orderByAsc(Area::getAdcode);
        List<Area> areas = baseMapper.selectList(wrapper);
        return BeanUtil.copyToList(areas,AreaVo.AreaListVo.class);
    }

    @Override
    public List<AreaVo.AreaListVo> listAreaByProvince(String adCode) {
        LambdaQueryWrapper<Area> wrapper = Wrappers.lambdaQuery();
        wrapper.select(Area::getAdcode,Area::getName).eq(Area::getParentAdcode,adCode).orderByAsc(Area::getAdcode);
        List<Area> areas = baseMapper.selectList(wrapper);
        return BeanUtil.copyToList(areas,AreaVo.AreaListVo.class);
    }
}
